home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ für Kids
/
C++ for kids.iso
/
SETUP
/
US
/
CBUILDER
/
DATA.Z
/
DEQUE.H
< prev
next >
Wrap
C/C++ Source or Header
|
1997-02-13
|
31KB
|
993 lines
/* $Revision: 8.1 $ */
/***************************************************************************
*
* deque - Declaration and definition for the Standard Library deque class
*
* $Id: deque,v 1.43 1995/09/29 04:16:00 hart Exp $
*
***************************************************************************
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
***************************************************************************
*
* (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
* ALL RIGHTS RESERVED
*
* The software and information contained herein are proprietary to, and
* comprise valuable trade secrets of, Rogue Wave Software, Inc., which
* intends to preserve as trade secrets such software and information.
* This software is furnished pursuant to a written license agreement and
* may be used, copied, transmitted, and stored only in accordance with
* the terms of such license and with the inclusion of the above copyright
* notice. This software and information or any other copies thereof may
* not be provided or otherwise made available to any other person.
*
* Notwithstanding any other lease or license that may pertain to, or
* accompany the delivery of, this computer software and information, the
* rights of the Government regarding its use, reproduction and disclosure
* are as set forth in Section 52.227-19 of the FARS Computer
* Software-Restricted Rights clause.
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7013.
* Contractor/Manufacturer is Rogue Wave Software, Inc.,
* P.O. Box 2328, Corvallis, Oregon 97339.
*
* This computer software and information is distributed with "restricted
* rights." Use, duplication or disclosure is subject to restrictions as
* set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
* Computer Software-Restricted Rights (April 1985)." If the Clause at
* 18-52.227-74 "Rights in Data General" is specified in the contract,
* then the "Alternate III" clause applies.
*
**************************************************************************/
#ifndef __STD_DEQUE__
#define __STD_DEQUE__
#include <stddefs.h>
#include <stdcomp.h>
#include <function>
#include <algorith>
#include <iterator>
#ifndef Allocator
#define Allocator allocator
#include <memory>
#endif
#ifndef deque
#define deque deque
#endif
#ifndef RWSTD_NO_NAMESPACE
namespace std {
#endif
template <class T>
class deque
{
public:
class iterator;
class const_iterator;
friend class iterator;
friend class const_iterator;
public:
typedef T value_type;
typedef Allocator<T> data_allocator_type;
typedef Allocator<T>::pointer pointer;
typedef Allocator<T>::reference reference;
typedef Allocator<T>::const_reference const_reference;
typedef Allocator<T>::size_type size_type;
typedef Allocator<T>::difference_type difference_type;
typedef Allocator<pointer> map_allocator_type;
protected:
data_allocator_type data_allocator;
map_allocator_type map_allocator;
typedef Allocator<pointer>::pointer map_pointer;
static size_type buffer_size ()
{
//
// Each time we allocate memory we reserve space for
// a chunk of objects of type T. This is currently tuned to
// allocate memory in 1K chunks, except for large objects.
//
return sizeof(T) >= 1024 ? 1 : 1024 / sizeof(T);
}
public:
//
// Definition of our iterator.
//
class iterator : public random_access_iterator<T, difference_type>
{
friend class deque<T>;
friend class const_iterator;
protected:
pointer current;
pointer first;
pointer last;
map_pointer node;
iterator (pointer x, map_pointer y)
: current(x), first(*y), last(*y + buffer_size()), node(y) {}
public:
iterator () : current(0), first(0), last(0), node(0) {}
reference operator* () const { return *current; }
difference_type operator- (const iterator& x) const
{
return node == x.node
? current - x.current
: difference_type(buffer_size() * (node - x.node - 1) +
(current - first) + (x.last - x.current));
}
iterator& operator++ ()
{
if (++current == last)
{
first = *(++node);
current = first;
last = first + buffer_size();
}
return *this;
}
iterator operator++ (int)
{
iterator tmp = *this; ++*this; return tmp;
}
iterator& operator-- ()
{
if (current == first)
{
first = *(--node);
last = first + buffer_size();
current = last;
}
--current;
return *this;
}
iterator operator-- (int)
{
iterator tmp = *this; --*this; return tmp;
}
iterator& operator+= (difference_type n)
{
difference_type offset = n + (current - first);
difference_type num_node_to_jump = offset >= 0
? offset / buffer_size()
: -((-offset + buffer_size() - 1) / buffer_size());
if (num_node_to_jump == 0)
current += n;
else
{
node = node + num_node_to_jump;
first = *node;
last = first + buffer_size();
current = first + (offset - num_node_to_jump * buffer_size());
}
return *this;
}
iterator& operator-= (difference_type n) { return *this += -n; }
iterator operator+ (difference_type n) const
{
iterator tmp = *this; return tmp += n;
}
iterator operator- (difference_type n) const
{
iterator tmp = *this; return tmp -= n;
}
reference operator[] (difference_type n) { return *(*this + n); }
bool operator== (const iterator& x) const
{
return current == x.current ||
((current == first || x.current == x.first) &&
*this - x == 0);
}
bool operator< (const iterator& x) const
{
return (node == x.node) ? (current < x.current) : (node < x.node);
}
}; // End of nested definiton of iterator.
//
// Definition of our constant iterator.
//
class const_iterator : public random_access_iterator<T, difference_type>
{
friend class deque<T>;
protected:
pointer current;
pointer first;
pointer last;
map_pointer node;
const_iterator (pointer x, map_pointer y)
: current(x), first(*y), last(*y + buffer_size()), node(y) {}
public:
const_iterator () : current(0), first(0), last(0), node(0) {}
const_iterator (const iterator& x)
: current(x.current), first(x.first), last(x.last), node(x.node) {}
const_reference operator* () const { return *current; }
difference_type operator- (const cons